import diceUtil
import character
from config import *


# 用于处理骰子指令
def dice(content):
    # 获得用户
    sender = content['sender']
    nickname = sender['nickname']
    user_id = sender['user_id']
    dic_num = 100
    message = content['message']
    cmd_msg = message
    arg = ''
    plus = 0
    if ' ' in message.strip():
        msgs = message.split(' ')
        arg = msgs[1].strip()
        cmd_msg = msgs[0].strip()

    if '+' in cmd_msg:
        addmsg = cmd_msg.split('+')
        cmd_msg = addmsg[0]
        plus = int(addmsg[1])

    if '-' in cmd_msg:
        dismsg = cmd_msg.split('-')
        cmd_msg = dismsg[0]
        plus = 0 - int(dismsg[1])

    cmd_msg = cmd_msg.replace('.r', '')
    if 'd' in cmd_msg:
        cmd_msg = cmd_msg.replace('d', '')
        if cmd_msg == '' or cmd_msg.isspace():
            dic_num = 20
        else:
            dic_num = int(cmd_msg)

    user_name = character.get_name(user_id)
    if user_name is None:
        user_name = nickname
    dis_value = diceUtil.range(dic_num)
    attr_plus = 0
    if arg in ATTRIBUTE:
        attr = character.get_attribute(user_id)
        value = attr[arg]
        attr_plus = int((value - 10) / 2)

    result = f'{user_name}:检定 {arg} 为{dis_value + int(plus) + int(attr_plus)}({dis_value}'

    print(plus)
    if plus >= 0:
        result += ' + ' + str(plus)
    else:
        result += ' - ' + str(-plus)
        print(attr_plus)
    if attr_plus >= 0:
        result += ' + ' + str(attr_plus)
    else:
        result += ' - ' + str(-attr_plus)
    result += ')'
    return result
